package com.education.welco.controller;

import com.education.common.core.BaseController;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.Convert;
import com.education.utils.BaiduAipContentCensor;
import com.education.welco.feign.userFeign;
import com.education.welco.pojo.CourseNote;
import com.education.welco.service.ICourseNoteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 课堂笔记 信息操作处理
 *
 * @author d
 * @date 2022-06-27
 */
@Slf4j
@RestController
@RequestMapping("/courseNote")
public class CourseNoteController extends BaseController {

    @Autowired
    private BaiduAipContentCensor baiduAipContentCensor;
    @Autowired
    private userFeign userFeign;
    @Autowired
    private ICourseNoteService courseNoteService;

    /**
     * 查询课堂笔记列表  根据用户id以及课堂id
     */
    @GetMapping("/list")
    public ApiReturnObject list(CourseNote courseNote, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<CourseNote> list = courseNoteService.selectCourseNoteList(courseNote);
        PageInfo pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200", "查询成功", pageInfo);
    }

    /**
     * 新增保存课堂笔记
     */
    @PostMapping("/add")
    public ApiReturnObject addSave(Integer courseId, String noteText, String noteImage, String userId) {
        //用户信息的处理
        CourseNote courseNote = new CourseNote();
        courseNote.setCourseId(courseId);
        courseNote.setNoteText(noteText);
        courseNote.setNoteImage(noteImage);
        //评论检测
        if (StringUtils.isNotBlank(noteText)) {
            int i = baiduAipContentCensor.AipText(noteText);
            if (i == 2) {
                //同时删除掉已经上传到七牛的资源
                List<String> strings = Arrays.asList(Convert.toStrArray(";", noteImage));
                QiniuUtil.deleteFileFromQinius(strings);
                return new ApiReturnObject("201", "您新增的课堂笔记中出现不当言论,请检查后再次提交", "您新增的课堂笔记中出现不当言论,请检查后再次提交");
            }
        }
        courseNote.setUserId(userId);
        //根据userId搜索user   //根据userId搜索user byUserId.get("name").toString(), byUserId.get("image").toString()
        Map<String, Object> byUserId = userFeign.findByUserId(Integer.valueOf(courseNote.getUserId()));
        courseNote.setUserIco(byUserId.get("image").toString());
        courseNote.setUserName(byUserId.get("name").toString());
        courseNote.setCreateBy(byUserId.get("name").toString());
        courseNote.setCreateTime(new Date());
        courseNoteService.insertCourseNote(courseNote);
        //批量图片检测
        if (StringUtils.isNotBlank(noteImage)) {
            baiduAipContentCensor.batchAipImage(noteImage, "course_note", courseNote.getId());
        }
        return new ApiReturnObject("200", "添加成功", "");
    }

    /**
     * 详情课堂笔记
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        CourseNote result = courseNoteService.selectCourseNoteById(id);
        return new ApiReturnObject("200", "查询成功", result);
    }

    /**
     * 修改保存课堂笔记
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody CourseNote courseNote) {
        courseNoteService.updateCourseNote(courseNote);
        return new ApiReturnObject("200", "修改成功", "");
    }

    /**
     * 删除课堂笔记
     */
    @PostMapping("/del")
    public ApiReturnObject remove(String ids) {
        courseNoteService.deleteCourseNoteByIds(ids);
        return new ApiReturnObject("200", "删除成功", "");
    }
}